package com.czh.mock.providers.text.datetime;

import com.czh.mock.exceptions.InvalidArgumentException;
import com.czh.mock.exceptions.code.MockCodeEnum;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

public class MockDatetime {
    private final DatetimeConfig config;

    public static MockDatetime build(DatetimeConfig config) {
        return new MockDatetime(config);
    }

    private MockDatetime(DatetimeConfig config) {
        this.config = config;
    }

    public long nextTime() {
        if (config.getMinTimestamp() == null && config.getMaxTimestamp() == null) {
            return RandomUtils.nextLong(DatetimeConfig.MIN_TIMESTAMP, DatetimeConfig.MAX_TIMESTAMP);
        }
        // 配置了时间范围的情况
        return RandomUtils.nextLong(config.getMinTimestamp(), config.getMaxTimestamp());
    }

    public String nextDatetime() {
        if (config.getType() == DatetimeConfig.DatetimeType.TIMESTAMP) {
            throw new InvalidArgumentException(MockCodeEnum.INVALID_ARGUMENT);
        }
        long time = this.nextTime();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochSecond(time), ZoneId.of("+8"));
        return localDateTime.format(DateTimeFormatter.ofPattern(config.getFormat()));
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(MockDatetime.build(
                    DatetimeConfig.build(
                            DatetimeConfig.DatetimeType.DATETIME, null,
                            "2025-01-01 00:00:00", "2025-12-31 23:59:59"
                            )
            ).nextDatetime());

//            System.out.println(RandomStringUtils.random(3));
            System.out.println(RandomStringUtils.randomAlphabetic(5));
//            System.out.println(RandomStringUtils.randomAlphanumeric(5));
//            System.out.println(RandomStringUtils.randomGraph(5));
//            System.out.println(RandomStringUtils.randomNumeric(5));
            System.out.println(RandomStringUtils.randomAscii(5));
        }
    }
}
